﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TraCuu_Update
{
    public partial class KhachHangGUI : UserControl,
        GUI.UctrlGUI, GUI.TraCuu_Update.GVPageInf
    {
        GUI.TraCuu_Update.GVPagesGUI _List = null;
        List<GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn> _columns = null;
        BLL.DanhSachKhachHangBLL _DanhSachKhacHangBLL = null;
        String[] _filter = null;
        bool _EnableEdit = false;
        Action<DTO.KhachHangDTO> _Callback = null;

        public KhachHangGUI(bool edit, Action<DTO.KhachHangDTO> callback)
        {
            InitializeComponent();

            _filter = new String[] { 
                String.Empty, // ten
                String.Empty, // diachi
                String.Empty, // dienthoai
                String.Empty  // email
            };
            _DanhSachKhacHangBLL = new BLL.DanhSachKhachHangBLL();
            _EnableEdit = edit;
            _Callback = callback;
        }

        public String getName()
        {
            return "TRA CỨU KHÁCH HÀNG";
        }

        public int getPages()
        {
            _filter[0] = txt_Ten.Text;
            _filter[1] = txt_DiaChi.Text;
            _filter[2] = txt_DienThoai.Text;
            _filter[3] = txt_Email.Text;

            return _DanhSachKhacHangBLL.CountMatchFilter(_filter);
        }

        public List<GUI.TraCuu_Update.GVPagesGUI.GVPagesItem> getItemsPerPage(int page)
        {
            List<DTO.KhachHangDTO> khs = 
                _DanhSachKhacHangBLL.MatchFilterPerPage(page, _filter);
            List<GUI.TraCuu_Update.GVPagesGUI.GVPagesItem> list = new List<GUI.TraCuu_Update.GVPagesGUI.GVPagesItem>();
            foreach (DTO.KhachHangDTO dto in khs)
            {
                GUI.TraCuu_Update.GVPagesGUI.GVPagesItem item = new GUI.TraCuu_Update.GVPagesGUI.GVPagesItem();
                item.Data = dto.ToStringList();
                list.Add(item);
            }
            return list;
        }

        public void processItemChange(List<GUI.TraCuu_Update.GVPagesGUI.GVPagesItem> items)
        {
            List<DTO.KhachHangDTO> itemsChage = new List<DTO.KhachHangDTO>();
            foreach (GUI.TraCuu_Update.GVPagesGUI.GVPagesItem item in items)
            {
                if (item.Edited == true)
                {
                    DTO.KhachHangDTO dto = new DTO.KhachHangDTO(item.Data);
                    itemsChage.Add(dto);
                }
            }
            _DanhSachKhacHangBLL.UpdateList(itemsChage);
        }

        private void KhachHangGUI_Load(object sender, EventArgs e)
        {
            txt_Ten.Text = "*";
            txt_Email.Text = "*";
            txt_DiaChi.Text = "*";
            txt_DienThoai.Text = "*";
            InitColums();
            pnl_List.Controls.Clear();
            pnl_List.Controls.Add(_List);
        }

        private void InitColums()
        {
            _columns = new List<GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn>();

            DataGridViewColumn clID =
                new DataGridViewColumn();
            clID.Name = "ID";
            clID.HeaderText = "ID";
            clID.Visible = false;
            clID.ReadOnly = true;
            _columns.Add(new GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn(clID,null));

            DataGridViewColumn clTen = 
                new DataGridViewColumn();
            clTen.Name = "HoTen";
            clTen.HeaderText = "Họ Tên";
            clTen.Visible = true;
            clTen.ReadOnly = !_EnableEdit;
            _columns.Add(new GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn(clTen, null));

            DataGridViewColumn clDiaChi = 
                new DataGridViewColumn();
            clDiaChi.Name = "DiaChi";
            clDiaChi.HeaderText = "Địa Chỉ";
            clDiaChi.Visible = true;
            clDiaChi.ReadOnly = !_EnableEdit;
            _columns.Add(new GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn(clDiaChi, null));

            DataGridViewColumn clDienThoai = 
                new DataGridViewColumn();
            clDienThoai.Name = "DienThoai";
            clDienThoai.HeaderText = "Điện Thoại";
            clDienThoai.Visible = true;
            clDienThoai.ReadOnly = !_EnableEdit;
            if (clDienThoai.ReadOnly == true)
                _columns.Add(new GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn(clDienThoai, null));
            else
                _columns.Add(new GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn(clDienThoai,
                    this.txt_DienThoai_Edit_KeyPress));

            DataGridViewColumn clEmail = 
                new DataGridViewColumn();
            clEmail.Name = "Email";
            clEmail.HeaderText = "Email";
            clEmail.Visible = true;
            clEmail.ReadOnly = !_EnableEdit;
            _columns.Add(new GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn(clEmail, null));

            DataGridViewColumn clDuNo =
                new DataGridViewColumn();
            clDuNo.Name = "SoLuong";
            clDuNo.HeaderText = "Dư Nợ";
            clDuNo.Visible = true;
            clDuNo.ReadOnly = !_EnableEdit;
            if (clDuNo.ReadOnly == true)
                _columns.Add(new GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn(clDuNo, null));
            else
                _columns.Add(new GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn(clDuNo, txt_DuNo_KeyPress));

            _List = new GUI.TraCuu_Update.GVPagesGUI(this, _columns, ProcessCallBack);
        }

        private void btn_Tim_Click(object sender, EventArgs e)
        {
            _List.Reload();
        }

        private void ProcessCallBack(List<String> l)
        {
            if (_Callback != null)
            {
                DTO.KhachHangDTO dto = new DTO.KhachHangDTO(l);
                _Callback(dto);
            }
        }

        private void txt_DienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) &&
                !char.IsDigit(e.KeyChar) &&
                e.KeyChar != '+' &&
                e.KeyChar != '*' &&
                e.KeyChar != '?')
                e.Handled = true;
            if (e.KeyChar == '+' &&
                (sender as TextBox).Text.IndexOf('+') > -1)
                e.Handled = true;
        }

        private void txt_DienThoai_Edit_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) &&
                !char.IsDigit(e.KeyChar) &&
                e.KeyChar != '+')
                e.Handled = true;
            if (e.KeyChar == '+' &&
                (sender as TextBox).Text.IndexOf('+') > -1)
                e.Handled = true;
        }

        private void txt_DuNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) &&
                !char.IsDigit(e.KeyChar) &&
                e.KeyChar != '.')
                e.Handled = true;
            if (e.KeyChar == '.' &&
                (sender as TextBox).Text.IndexOf('.') > -1)
                e.Handled = true;
        }

        public void SetCallback(Action<DTO.KhachHangDTO> callback)
        {
            _Callback = callback;
        }
    }
}
